package com.usian.controller;

import com.usian.feign.SsFeign;
import com.usian.pojo.TbUser;
import com.usian.utils.Result;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("frontend/sso")
public class SSOController {
    @Autowired
    private SsFeign ssFeign;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @RequestMapping("checkUserInfo/{checkValue}/{checkFlag}")
    public Result checkUserInfo(@PathVariable String checkValue,@PathVariable Integer checkFlag){
        Boolean tbUser=ssFeign.checkUserInfo(checkValue,checkFlag);
        if (tbUser){
            return Result.ok();
        }
        return Result.error("校验失败");

    }
    @RequestMapping("userRegister")
    public Result userRegister(TbUser tbUser){
        int num=ssFeign.userRegister(tbUser);
        if (num==1){
            return Result.ok();
        }
        return Result.error("注册失败");
    }

    @RequestMapping("userLogin")
    public Result userLogin(@RequestParam String username,@RequestParam  String password){
        Map map=ssFeign.userLogin(username,password);
        if (map!=null){
            amqpTemplate.convertAndSend("gouwuche", "gouwuche.tongbu",map.get("userid"));

            return Result.ok(map);
        }
        return Result.error("登录失败");
    }

    @RequestMapping("logOut")
    public Result logOut(@RequestParam  String token){
       Boolean boo=ssFeign.logOut(token);
        if (boo){
            return Result.ok();
        }
        return Result.error("退出登陆失败");
    }

    @RequestMapping("getUserByToken/{token}")
    public Result getUserByToken(@PathVariable String token){
        TbUser tbUser=ssFeign.getUserByToken(token);
        if (tbUser!=null){
            return Result.ok();
        }
        return Result.error("登陆过期");
    };



}
